import Router from 'koa-router'
import { validator, atLeastOneParam } from '../middleware/common.middleware.ts'
import {
  getCarpoolMember,
  addCarpoolMember,
  editCarpoolMember,
  removeCarpoolMember,
} from '../controller/carpoolmember.controller.ts'

const router = new Router({ prefix: '/carpoolmember' })

// 查询拼车成员
router.get(
  '/get',
  validator({
    carpoolMemberIds: { type: 'string', min: 1, required: true },
  }),
  getCarpoolMember,
)

// 新增拼车成员
router.post(
  '/add',
  validator({
    memberName: { type: 'string', min: 1, max: 255, required: true },
    startDate: { type: 'dateTime', required: true },
    endDate: { type: 'dateTime', required: true },
    email: {
      type: 'string',
      min: 1,
      max: 255,
      allowEmpty: true,
      required: false,
    },
    expire: {
      type: 'string',
      min: 1,
      max: 1,
      allowEmpty: true,
      required: false,
    },
    remark: {
      type: 'string',
      min: 1,
      max: 255,
      allowEmpty: true,
      required: false,
    },
    carpoolType: { type: 'string', min: 1, max: 255, required: true },
  }),
  addCarpoolMember,
)

// 修改拼车成员
router.put(
  '/update',
  validator({
    carpoolMemberId: { type: 'string', min: 36, max: 36, required: true },
    memberName: {
      type: 'string',
      min: 1,
      max: 255,
      allowEmpty: true,
      required: false,
    },
    startDate: { type: 'dateTime', allowEmpty: true, required: false },
    endDate: { type: 'dateTime', allowEmpty: true, required: false },
    email: { type: 'string', max: 255, allowEmpty: true, required: false },
    expire: {
      type: 'string',
      min: 1,
      max: 1,
      allowEmpty: true,
      required: false,
    },
    remark: { type: 'string', max: 255, allowEmpty: true, required: false },
    carpoolType: {
      type: 'string',
      min: 36,
      max: 36,
      allowEmpty: true,
      required: false,
    },
  }),
  atLeastOneParam([
    'memberName',
    'startDate',
    'endDate',
    'email',
    'expire',
    'remark',
    'carpoolType',
  ]),
  editCarpoolMember,
)

// 删除拼车成员
router.delete(
  '/delete/:carpoolMemberId',
  validator({
    carpoolMemberId: { type: 'string', min: 36, max: 36, required: true },
  }),
  removeCarpoolMember,
)

export default router
